import {
  _decorator,
  Component,
  instantiate,
  Label,
  Node,
  RigidBody,
  RigidBody2D,
  Script,
} from "cc";
const { ccclass, property } = _decorator;
import { Home } from "../Home/Home";

@ccclass("FruitNode")
export class FruitNode extends Component {
  //当前水果的编号
  @property(Number)
  fruitType: number;
  //刚体组件
  private myRigidBody: RigidBody2D = null;
  //检查间隔时间
  private _time: number = 0.5;
  //GameBox
  @property(Node)
  GameBox: Node;
  //游戏脚本实例
  private GameTS: Home;
  protected onLoad(): void {
    this.myRigidBody = this.node.getComponent(RigidBody2D);
    // 每隔0.5秒检查一次刚体的位置
    this.schedule(this.checkPositionChange, this._time);
  }
  start() {
    //获取游戏脚本实力
    this.GameTS = this.GameBox.getComponent(Home);
  }

  checkPositionChange() {
    if (this.GameTS._gameOver) return;
    //获取节点的高度
    let height: number = Number(this.node.height);
    // 获取刚体的位置顶部的位置
    const currentPositionY = this.myRigidBody.node.position.y + height / 2;
    // 在这里可以进行位置变化的判断逻辑
    // console.log("Current position:" + this.fruitType, currentPositionY);
    //判断是否终止游戏
    if (currentPositionY > -100) {
      this.GameTS._gameOver = true;
      // console.log("游戏结束");
      //结果弹窗
      this.GameTS.overPopup.getChildByName("score").getComponent(Label).string =
        "总得分：" + this.GameTS.ScoreLab.string;
      this.GameTS.overPopup.active = true;
      this.scheduleOnce(() => {
        // this.GameBox.destroy();
      }, 0);
    }
  }
  update(deltaTime: number) {}
}
